package cn.wolfcode.crm.service.imp;

import cn.wolfcode.crm.domain.Depot;
import cn.wolfcode.crm.mapper.DepotMapper;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDepotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class DepotServiceImpl implements IDepotService {
    @Autowired
    private DepotMapper depotMapper;


    @Override
    public void deleteByPrimaryKey(Long id) {
        depotMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveOrUPdate(Depot depot) {
        if (depot.getId()==null) {
            depot.setInputTime(new Date());
            depot.setStatus(true);
            depotMapper.insert(depot);
        }else {
            depotMapper.updateByPrimaryKey(depot);
        }
    }

    @Override
    public Depot selectByPrimaryKey(Long id) {
        return depotMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Depot> selectAll() {
        return depotMapper.selectAll();
    }

    @Override
    public PageResult query(QueryObject qo) {
        int total = depotMapper.selectCount(qo);
        if(total==0){
            return new PageResult(total, Collections.EMPTY_LIST);
        }
        List<Depot> rows = depotMapper.queryForList(qo);
        return new PageResult(total,rows);
    }

    @Override
    public void changeState(Long id) {
        depotMapper.changeState(id);
    }
}
